#include "com_rt_rtweibo_UiAppView.h"

#include <jni.h>
#include <time.h>
#include <android/log.h>
#include <string.h>

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <pthread.h>
#include <unistd.h>

#include "RtBase.h"
#include "RtFile.h"
#include "RtControl.h"
#include "RtWindow.h"
#include "RtWindowGc.h"
#include "RtUiManager.h"
#include "RtTimer.h"
#include "RtTimerManager.h"
#include "RtAndroidImpl.h"
#include "RtLog.h"
#include "RtEnv.h"
#include "RtUiManager.h"
#include "RtUiManagerObserver.h"
#include "RtPrivateData.h"
#include "RtEdit.h"

class CRtMainAppImpl;

extern JavaVM* g_jvm;
extern JNIEnv* g_env;
extern jobject g_view;

extern JNIEnv* g_envLocal;
extern jobject g_viewLocal;

int g_nScreenWidth;
int g_nScreenHeight;

CRtMainAppImpl* g_pAppImpl=NULL;
CRtUiManager* m_pHanglvApp=NULL;

CRtUiManager* CreateUiManager();
//
class CRtMainAppImpl : public MRtUiManagerObserver
	{
public:
	virtual void HandleUiExit();
	virtual void NotifyDraw();
	virtual void Create2SendSms(const char* pText,int nLen);
	virtual void MakeNewCall(const char* pText,int nLen);
	};

void CRtMainAppImpl::HandleUiExit()
	{
	jclass clsView = g_env->GetObjectClass(g_view);
	jmethodID func_id = g_env->GetMethodID(clsView, "postExitApp", "()V");
	g_env->CallVoidMethod(g_view, func_id);

	g_env->DeleteLocalRef(clsView);
	}

void CRtMainAppImpl::NotifyDraw()
	{
	if(g_env)
		{
		jclass clsView = g_env->GetObjectClass(g_view);
		jmethodID func_id = g_env->GetMethodID(clsView, "postInvalidate", "()V");
		g_env->CallVoidMethod(g_view, func_id);

		g_env->DeleteLocalRef(clsView);
		}
	}

void CRtMainAppImpl::Create2SendSms(const char* pText,int nLen)
	{
	
	}

void CRtMainAppImpl::MakeNewCall(const char* pText,int nLen)
	{
	
	}

/*
 * Class:     com_rt_rtweibo_UiAppView
 * Method:    SetViewWindow
 * Signature: (Landroid/view/View;)V
 */
JNIEXPORT void JNICALL Java_com_rt_rtweibo_UiAppView_SetViewWindow(JNIEnv* env, jobject thiz, jobject viewWindow)
{
	g_env=env;
	g_view=env->NewGlobalRef(viewWindow);
	env->GetJavaVM(&g_jvm);

	//SetJvm(g_jvm);
	//SetAndroidEnv(g_env,g_view);
}

/*
 * Class:     com_rt_rtweibo_UiAppView
 * Method:    SetScreenSize
 * Signature: (II)V
 */
JNIEXPORT void JNICALL Java_com_rt_rtweibo_UiAppView_SetScreenSize(JNIEnv* env, jobject thiz, jint nWidth, jint nHeight)
	{
	g_nScreenWidth=nWidth;
	g_nScreenHeight=nHeight;
	SetScreenSize(g_nScreenWidth,g_nScreenHeight);

	//if(m_pHanglvApp)
	//	{
	//	CRtRect rect(0,0,g_nScreenWidth,g_nScreenHeight);
	//	m_pHanglvApp->SetScreenRect(rect);
	//	m_pHanglvApp->SetUiRect(rect);
	//	}
	}

/*
 * Class:     com_rt_rtweibo_UiAppView
 * Method:    SetScreenDPI
 * Signature: (II)V
 */
JNIEXPORT void JNICALL Java_com_rt_rtweibo_UiAppView_SetScreenDPI(JNIEnv* env, jobject thiz, jint nDpi)
	{
	SetScreenDPI2(nDpi);
	}


/*
 * Class:     com_rt_rtweibo_UiAppView
 * Method:    StartApp
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_rt_rtweibo_UiAppView_StartApp(JNIEnv* env, jobject thiz)
	{
	DBGPRINTF("Start app");
	g_pAppImpl=new CRtMainAppImpl();
	
	m_pHanglvApp=CreateUiManager();
	m_pHanglvApp->SetObserver(g_pAppImpl);
	m_pHanglvApp->SetOsType(ERtOs_android);
	m_pHanglvApp->SetLayoutType(ERtLayout_PAD);
	
	CRtWindow* pWnd=new CRtWindow(g_view);
	CRtControl* pParent=new CRtControl(pWnd);
	pParent->SetRect(CRtRect(0,0,g_nScreenWidth,g_nScreenHeight));
	
	m_pHanglvApp->SetScreenRect(CRtRect(0,0,g_nScreenWidth,g_nScreenHeight));
	m_pHanglvApp->SetPlatWindow(pParent);
	m_pHanglvApp->Start();
	DBGPRINTF("Start app finish");
 
	}

JNIEXPORT void JNICALL Java_com_rt_rtweibo_UiAppView_StopApp(JNIEnv* env, jobject thiz)
{
	DBGPRINTF("Clean resource");

	if(m_pHanglvApp)
	{
		m_pHanglvApp->Release();
		m_pHanglvApp=NULL;
	}
	if(g_pAppImpl)
	{
		delete g_pAppImpl;
		g_pAppImpl=NULL;
	}
}

/*
 * Class:     com_rt_rtweibo_UiAppView
 * Method:    timerFinished
 * Signature: (I)V
 */
JNIEXPORT void JNICALL Java_com_rt_rtweibo_UiAppView_TimerFinished(JNIEnv* env, jobject thiz, jint nTimerId)
    {
	CRtTimerManager& timerMgr=CRtEnv::TimerMgr();

	for(int i=0;i<timerMgr.m_plstTimer->GetCount();i++)
	{
		CRtTimer* pTimer=(CRtTimer*)timerMgr.m_plstTimer->GetAt(i);
		if(pTimer->m_nID==nTimerId)
		{
			if(pTimer->m_nType==1)
				;
			else
				pTimer->Stop();

			if(pTimer->m_pObserver)
				{
				pTimer->m_pObserver->TimerFinished(pTimer->m_nID);
				}
			break;
		}
	}
    }

JNIEXPORT void JNICALL Java_com_rt_rtweibo_UiAppView_DrawData(JNIEnv* env, jobject thiz,
		jbyteArray aBuf, jint nWidth, jint nHeight)
{
	unsigned char* p=(unsigned char*)env->GetByteArrayElements(aBuf,0);

	CRtWindowGc gc;
    gc.SetBuf(p,nWidth,nHeight,nWidth*4);

	if(m_pHanglvApp)
		m_pHanglvApp->UiDraw(gc,CRtRect(0,0,nWidth,nHeight));

	env->ReleaseByteArrayElements(aBuf, (jbyte*)p, 0);  
}

/*
 * Class:     com_rt_rtweibo_UiAppView
 * Method:    HandleTouchEvent
 * Signature: (IIII)Z
 */
JNIEXPORT void JNICALL Java_com_rt_rtweibo_UiAppView_HandleTouchEvent
  (JNIEnv* env, jobject thiz, jint nEvtType, jint nModifiers, jint ptX, jint ptY)
{
	g_envLocal=env;
	g_viewLocal=thiz;

	 TRtPointerEvent evtPointer;
	 
	 if(nEvtType==1)
		 evtPointer.uiType=TRtPointerEvent::EButton1Down;
	 else if(nEvtType==2)
		 evtPointer.uiType=TRtPointerEvent::EMove;
	 else if(nEvtType==3)
		 evtPointer.uiType=TRtPointerEvent::EButton1Up;
	 
	 evtPointer.uiModifiers=nModifiers;
	 evtPointer.iPosition.m_nX=ptX;
	 evtPointer.iPosition.m_nY=ptY;
	 evtPointer.iParentPosition.m_nX=0;
	 evtPointer.iParentPosition.m_nY=0;
	 
	if(m_pHanglvApp)
		m_pHanglvApp->HandlePointerEventL(evtPointer);
}

/*
 * Class:     com_rt_rtweibo_UiAppView
 * Method:    HandleKeyEvent
 * Signature: (III)Z
 */
JNIEXPORT jboolean JNICALL Java_com_rt_rtweibo_UiAppView_HandleKeyEvent
  (JNIEnv* env, jobject thiz, jint nKeyCode, jint nRepCount, jint nAction)
{
	if(!m_pHanglvApp)
		return false;
	
	TRtKeyEvent keyEvent;
	keyEvent.iCode=nKeyCode;
	keyEvent.iRepeats=nRepCount;
	keyEvent.iScanCode=nKeyCode;
	
	if(nAction==1)
		m_pHanglvApp->HandleKeyEvent(keyEvent,ERtEventKeyDown);
	else if(nAction==2)
		m_pHanglvApp->HandleKeyEvent(keyEvent,ERtEventKey);
	else if(nAction==3)
		m_pHanglvApp->HandleKeyEvent(keyEvent,ERtEventKeyUp);
}

JNIEXPORT void JNICALL Java_com_rt_rtweibo_UiAppView_SetEditText
  (JNIEnv* env, jobject thiz, jint editId, jstring str)
{
	CRtEdit* pEdit=NULL;
	bool bFind=false;
	
	CRtObjectList* pData=CRtEnv::PrivateData()->m_pEditList;
	for(int i=0;i<pData->GetCount();i++)
		{
		pEdit=(CRtEdit*)pData->GetAt(i);
		if(pEdit->GetID()==editId)
			{
			bFind=true;
			break;
			}
		}

	if(bFind && pEdit)
	{
		jboolean b=true;
		const char* pText=env->GetStringUTFChars(str,&b);
		pEdit->SetText(pText);
	}
}

